/**
 * Created by Administrator on 2017/4/20.
 */


var Poker = require('./Poker');

/**
 * 玩家手牌
 * @param owner
 * @constructor
 */
function HandCards() {
    this.cards = {};        // 手牌{card:num}
    this.initCards = [];   // 初始化时的牌
    this.lastCard = 0;     // 上次摸的牌
}

HandCards.prototype = {
    // 初始化
    init: function (cardArr) {
        for (var i = 0, m = cardArr.length; i < m; ++i) {
            ExObject.addNumber(this.cards, cardArr[i], 1);
            this.initCards.push(cardArr[i]);
        }
    },

    /**
     * 重置
     */
    reset: function() {
        this.cards = {};
        this.initCards = [];
        this.lastCard = 0;
    },

    /**
     * 获取牌
     * @returns {{}|*}
     */
    getCards: function() {
        return this.cards;
    },

    getInitCards: function() {
        return this.initCards;
    },

    /**
     * 获取牌张数
     * @param card
     * @returns {*|number}
     */
    getCardNum: function(card) {
        if (card) {
            return this.cards[card] || 0;
        }

        return ExObject.sumValue(this.cards);
    },

    /**
     * 是否为空，即没有牌
     * @returns {boolean}
     */
    isEmpty: function() {
        return this.getCardNum() <= 0;
    },

    /**
     * 获取上次摸的牌
     * @returns {number|*}
     */
    getLastNewCard: function() {
        return this.lastCard;
    },

    /**
     * 获取起手牌的最后一张牌
     * @returns {number|*}
     */
    getInitLastCard: function() {
        return this.initCards.length > 0 ? this.initCards[this.initCards.length - 1] : 0.
    },

    /**
     * 摸牌，将增加1张牌
     * @param card
     */
    addNewCard: function(card) {
        ExObject.addNumber(this.cards, card, 1);
        this.lastCard = card;
    },

    /**
     * 增加1张牌
     * @param card
     * @param num 数量，默认为1
     */
    addCard: function(card, num) {
        ExObject.addNumber(this.cards, card, num || 1);
    },

    /**
     * 删除1张牌
     * @param card
     */
    delCard: function(card) {
        var num = this.cards[card];
        if (num) {
            if (num <= 1) {
                delete this.cards[card];
            } else {
                this.cards[card] = num - 1;
            }
            return true;
        }
        return false;
    },
};


exports.HandCards = HandCards;